/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Action;

import Aptech.Common.BaseDispatchAction;
import Aptech.Common.Formater;
import Aptech.DAL.MgDocument;
import Aptech.DALInterface.DocumentInterface;
import Aptech.Service.SV_Buitls;
import Aptech.Service.SV_CatalogyDoc;
import Aptech.Service.SV_Documents;
import Aptech.Service.SV_Lands;
import Form.BuiltsForm;
import Form.DocForm;
import HibernateMappingPoJo.Builts;
import HibernateMappingPoJo.Documnets;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
 * 
 * @author nkhoan
 */
public class DocAction extends BaseDispatchAction {

	/* forward name="success" path="" */

	private SV_Documents mgDoc = new SV_Documents();
	private SV_Buitls mgBuilt = new SV_Buitls();
	private SV_Lands mgLand = new SV_Lands();
	private DocForm docForm;
	private String filePath;

	public ActionForward addDoc(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// set Catelogy of Document

		docForm = (DocForm) form;
		filePath = request.getServletContext().getRealPath("/") + "DataFile";
		request.setAttribute("arrCatelogyDoc", mgDoc.getAllCatelogyDoc());
		request.setAttribute("arrLands", mgLand.getAllLand());
		request.setAttribute("arrBuilts", mgBuilt.getAllBuilt());
		request.setAttribute("sitemap", "Add New A Docummet");

		String type = request.getParameter("type");
		if (type == null) {
			request.setAttribute("type", "add");
			return map.findForward("edit");
		} else {

			// FormFile myFile = docForm.getAttachFile();
			UUID id = UUID.randomUUID();
			Documnets doc = new Documnets();
			doc.setId(id.toString());

			doc.setIdCatalogyDoc(docForm.getId_CatalogyDoc());
			doc.setContent(docForm.getContent());
			doc.setName(docForm.getName());
			doc.setNote(docForm.getNote());
			if (docForm.getId_CatalogyDoc().equals("built")) {
				doc.setIdType(docForm.getId_Built());
			} else if (docForm.getId_CatalogyDoc().equals("land")) {
				doc.setIdType(docForm.getId_Land());
			} else {
				doc.setIdType(" ");
			}

			// extention of file
			String ext = "";
			if (!docForm.getAttachFile().getFileName().equals("")) {
				int dot = docForm.getAttachFile().getFileName()
						.lastIndexOf(".");
				ext = id + docForm.getAttachFile().getFileName().substring(dot);
				mgDoc.saveFile(ext, filePath, docForm.getAttachFile()
						.getFileData());
			}

			// chi luu ten file tren server
			doc.setAttachFile(ext);
			mgDoc.addDoc(doc);
			request.setAttribute("save", "save");
		}
		return map.findForward("edit");

	}

	public ActionForward getAllDocument(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		docForm = (DocForm) form;
		docForm.setArrDoc(mgDoc.getAllDocument());
		request.setAttribute("sitemap", "List of Docummet");
		return map.findForward("list");
	}

	public ActionForward editDocuments(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		docForm = (DocForm) form;
		String id = request.getParameter("id");
		request.setAttribute("sitemap", "Edit A Docummet");
		String type = request.getParameter("type");
		if (type.equals("edit1")) {
			request.setAttribute("id", id);
			request.setAttribute("type", "edit");
			request.setAttribute("arrCatelogyDoc", mgDoc.getAllCatelogyDoc());
			request.setAttribute("arrLands", mgLand.getAllLand());
			request.setAttribute("arrBuilts", mgBuilt.getAllBuilt());
			// Set value for Form
			Documnets doc = mgDoc.getDocByID(id);
			docForm.setName(doc.getName());
			docForm.setNote(doc.getNote());
			docForm.setContent(doc.getContent());
			docForm.setId_CatalogyDoc(doc.getIdCatalogyDoc());

			if (doc.getIdCatalogyDoc().equals("built")) {
				docForm.setId_Built(doc.getIdType());
			} else if (docForm.getId_CatalogyDoc().equals("land")) {
				docForm.setId_Land(doc.getIdType());
			}
			// docForm.setAttachFile(doc.getAttachFile());

			docForm.setId(id);
		} else {
			request.setAttribute("arrCatelogyDoc", mgDoc.getAllCatelogyDoc());
			request.setAttribute("arrLands", mgLand.getAllLand());
			request.setAttribute("arrBuilts", mgBuilt.getAllBuilt());
			request.setAttribute("type", "edit");

			Documnets doc = new Documnets();
			doc.setId(docForm.getId());
			doc.setIdCatalogyDoc(docForm.getId_CatalogyDoc());
			doc.setContent(docForm.getContent());
			doc.setName(docForm.getName());
			if (docForm.getId_CatalogyDoc().equals("built")) {
				doc.setIdType(docForm.getId_Built());
			} else if (docForm.getId_CatalogyDoc().equals("land")) {
				doc.setIdType(docForm.getId_Land());
			} else {
				doc.setIdType(" ");
			}
			doc.setNote(docForm.getNote());

			// xoa file cu
			filePath = request.getServletContext().getRealPath("/")
					+ "DataFile";
			String fileName = mgDoc.getDocByID(id).getAttachFile();
			if (!fileName.equals("")) {
				mgDoc.deleteFile(filePath, fileName);
			}

			// save file moi
			String ext = "";
			if (!docForm.getAttachFile().getFileName().equals("")) {
				int dot = docForm.getAttachFile().getFileName()
						.lastIndexOf(".");
				ext = id + docForm.getAttachFile().getFileName().substring(dot);
				mgDoc.saveFile(ext, filePath, docForm.getAttachFile()
						.getFileData());
			}

			doc.setAttachFile(ext);
			mgDoc.editDocuments(doc);
			request.setAttribute("save", "save");
		}

		return map.findForward("edit");
	}

	public ActionForward deleteFile(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String id = request.getParameter("id");
		filePath = request.getServletContext().getRealPath("/") + "DataFile";

		String fileName = mgDoc.getDocByID(id).getAttachFile();
		if (!fileName.equals("")) {
			mgDoc.deleteFile(filePath, fileName);
		}
		mgDoc.deleteDoc(id);
		response.sendRedirect("/NTB/doc.do?method=getAllDocument");
		return map.findForward("list");

	}

	public ActionForward detailDoc(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		docForm = (DocForm) form;
		String id = request.getParameter("id");
		request.setAttribute("sitemap", "Information Detail A Docummet");
		request.setAttribute("id", id);
		request.setAttribute("type", "detail");
		request.setAttribute("arrCatelogyDoc", mgDoc.getAllCatelogyDoc());
		request.setAttribute("arrLands", mgLand.getAllLand());
		request.setAttribute("arrBuilts", mgBuilt.getAllBuilt());
		// Set value for Form
		Documnets doc = mgDoc.getDocByID(id);
		docForm.setName(doc.getName());
		docForm.setNote(doc.getNote());
		docForm.setContent(doc.getContent());
		docForm.setId_CatalogyDoc(doc.getIdCatalogyDoc());
		if (doc.getIdCatalogyDoc().equals("built")) {
			docForm.setId_Built(doc.getIdType());
		} else if (docForm.getId_CatalogyDoc().equals("land")) {
			docForm.setId_Land(doc.getIdType());
		}
		// docForm.setAttachFile(doc.getAttachFile());
		docForm.setId(id);
		if(!doc.getAttachFile().equals("")){
			request.setAttribute("down", "down");
		}

		return map.findForward("edit");
	}

	public ActionForward downloadFile(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String id = request.getParameter("id");
		String fileName = mgDoc.getDocByID(id).getAttachFile();
		mgDoc.downloadFile("http://localhost:8080/NTB/DataFile/" + fileName,
				fileName, "D:/");

		response.sendRedirect("/NTB/doc.do?method=getAllDocument");
		return map.findForward("list");

	}
}
